package dom;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;


/**
 * 
 * @author Alex
 */
public class Predicate {
	private int id;
	private String name;
	private List<String> facts;
	
	/**
	 * For 
	 * @param id
	 * @param name
	 */
	public Predicate(int id, String name){
		this.id = id;
		this.name = name;
		facts = new ArrayList<String>();
	}
	
	/**
	 * @param id
	 */
	public Predicate(int id){
		this.id = id;
		this.name = "";
	}
	
	public Predicate(Predicate p) {
		id = p.id;
		name = p.name;
		facts = new LinkedList<String>(p.facts);
	}

	public void addFact(String name){
		facts.add(name);
	}
	public List<String> getFacts(){
		return facts;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public Predicate clone(){
		return new Predicate(this);
	}
	
	public boolean equals(Object o){
		if(o instanceof Predicate){
			Predicate other = new Predicate ((Predicate) o);
			if (	id != other.id || !name.equals(other.name) || facts.size() != other.facts.size()){
				return false;
			}
			ListIterator<String> otherFactIter = other.facts.listIterator();
			while(otherFactIter.hasNext()){
				if(facts.contains(otherFactIter.next())){
					otherFactIter.remove();
				}else{
					return false;
				}
			}
			if(!other.facts.isEmpty()){
				return false;
			}
			return true;
		}else{
			return false;
		}
		
	}

	@Override
	public String toString() {
		return "Predicate [id=" + id + ", name=" + name + ", facts=" + facts
				+ "]";
	}
	
}
